#include <sys/types.h> 
#include <sys/socket.h>
#include "../Include/InetSocket.h"
#include "../Include/SocketUtil.h"

CInetSocket::CInetSocket()
{
}

CInetSocket::CInetSocket(Int32 nFD) : CSocket(nFD)
{
}

Bool CInetSocket::ReuseAddress()
{
	return SocketUtil::ReuseAddress(m_nFD);
}

Bool CInetSocket::Connect(const CSocketAddr &address)
{
    EAddressFamily af = address.GetAddressFamily();
    if (af == AF_IPv4)
    {
        struct sockaddr_in addrRemote = address.GetSocketAddr();
        return connect(m_nFD, (struct sockaddr *)&addrRemote, sizeof(struct sockaddr)) == 0;
    }
    else if (af == AF_IPv6)
    {
        struct sockaddr_in6 addrRemote = address.GetSocketAddr6();
        return connect(m_nFD, (struct sockaddr *)&addrRemote, sizeof(struct sockaddr_in6)) == 0;
    }
    else
    {
        return False;
    }
}

Bool CInetSocket::Bind(const CSocketAddr &address)
{
    EAddressFamily af = address.GetAddressFamily();
    if (af == AF_IPv4)
    {
        struct sockaddr_in addrBind = address.GetSocketAddr();
        return bind(m_nFD, (struct sockaddr *)&addrBind, sizeof(struct sockaddr)) == 0;
    }
    else if (af == AF_IPv6)
    {
        struct sockaddr_in6 addrBind = address.GetSocketAddr6();
        return bind(m_nFD, (struct sockaddr *)&addrBind, sizeof(struct sockaddr_in6)) == 0;
    }
    else
    {
        return False;
    }
}
